package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.v2.dialog.ContactCustomerDialog;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.view.HorizontalProgressBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/31 14:35
 */
public class UpdateDialog extends BaseDialog implements View.OnClickListener {
    private final String TAG = "UpdateDialog";
    @BindView(R.id.version_name_tv)
    TextView versionNameTv;
    @BindView(R.id.version_depict_tv)
    TextView versionDepictTv;
    @BindView(R.id.progressbar)
    HorizontalProgressBar progressbar;
    @BindView(R.id.start_download_tv)
    TextView start_download_tv;
    @BindView(R.id.progress_layout)
    LinearLayout progress_layout;
    @BindView(R.id.close_iv)
    View close_iv;
    @BindView(R.id.contact_tv)
    TextView contact_tv;


    private int state; //0未开始1进行中2已完成


    public UpdateDialog(@NonNull Fragment context) {
        super(context);
    }

    public UpdateDialog(Activity context) {
        super(context);
    }

    @Override
    protected void init() {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        progressbar.setTotalProgress(100);
        progress_layout.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_appupdate;
    }


    @Override
    public void onClick(View v) {
        if (onClickListener != null) {
            onClickListener.onClick(this, 0);
        }
    }

    @OnClick(R.id.close_iv)
    void close_iv() {
        dismiss();
    }

    @OnClick(R.id.contact_tv)
    void contact_tv() {
        try {
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=2173766041";
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (Exception e) {
            ContactCustomerDialog customerDialog = new ContactCustomerDialog(activity);
            customerDialog.show();
        }
    }

    public void showCloseButton(boolean b) {
        close_iv.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置进度百分值
     *
     * @param progress
     */
    public void setProgress(int progress) {
        progressbar.setCurrentProgress(progress);
    }

    public void setText(String text) {
        progressbar.setTextString(text);
    }

    public void reset() {
        setProgress(0);

    }


    public void setState(int state) {
        this.state = state;
        switch (state) {
            //未开始
            case 0:
                //完成
            case 2:
                progressbar.setVisibility(View.GONE);
                start_download_tv.setVisibility(View.VISIBLE);
                break;
            //进行中
            case 1:
                progressbar.setVisibility(View.VISIBLE);
                start_download_tv.setVisibility(View.GONE);
                break;

        }
    }

    public int getState() {
        return state;
    }

    public void setVersionName(String versionName) {
        this.versionNameTv.setText("新版本 " + versionName);
    }

    public void setVersionDepict(String versionDepict) {
        this.versionDepictTv.setText(versionDepict);
    }
}
